package car.aistar.controller;

import car.aistar.dto.Result;
import car.aistar.entity.*;
import car.aistar.service.*;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName InforController
 * @Description TODO
 * @Author 肖
 * @Date 2020/10/2 16:27
 * Version 1.0
 **/
@Controller
@RequestMapping("/cardetail")
public class InforController {
    @Autowired
    private CarDetailService carDetailService;
    @Autowired
    private SellerService sellerService;
    @Autowired
    private CarBrandsService carBrandsService;
    @Autowired
    private CarStyleService carStyleService;
    @Autowired
    private PictureService pictureService;

    @Autowired
    private CarCartService carCartService;
    @Autowired
    private SellReserveService sellReserveService;
    @Autowired
    private ReserveService reserveService;
    @Autowired
    private PriceCutService priceCutService;
    @Autowired
    private UserRecordsService userRecordsService;

    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页的数据展示（根据车辆ID进行取值）
    *@Date 22:06 2020/10/8
    *@Param [model, carDetail]
    *@return java.lang.String
    **/
    @RequestMapping("/infor")
    public String infor(Model model, CarDetail carDetail, HttpServletRequest request){

        Integer id = (Integer) request.getSession().getAttribute("uid");

        if (id != null){
            UserCollect userCollect =userCollectService.queryByUId(Long.valueOf(id),carDetail.getId());
            PriceCut priceCut = priceCutService.queryByUId(Long.valueOf(id),carDetail.getId());
            if (priceCut != null){
                model.addAttribute("lingdang","成功提醒");
            }else {
                model.addAttribute("lingdang","降价提醒");
            }
            if (userCollect != null){
                model.addAttribute("collect","yshouchang");
            }else{
                model.addAttribute("collect","shouchang");
            }
                    String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            UserRecords userRecords = new UserRecords();
            userRecords.setUId(Long.valueOf(id));
            userRecords.setCId(carDetail.getId());
            java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
            userRecords.setRecordTime(date);
            userRecords.setUIp(ip);
            userRecordsService.insert(userRecords);

        }else {
            model.addAttribute("collect","shouchang");
            model.addAttribute("lingdang","降价提醒");

        }

        CarDetail carDetail1 = carDetailService.queryById(carDetail.getId());
        CarBrands carBrands = carBrandsService.queryById(carDetail1.getBrId());

        CarStyle carStyle = carStyleService.queryById(carDetail1
                .getStId());

       Seller seller = sellerService.queryById(Long.valueOf(carDetail1.getSalId()));
//       Seller seller = sellerService.queryById(3L);

        Picture picture = pictureService.queryByCId(carDetail.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1=sdf.format(carDetail1.getCInsure());


        model.addAttribute("insure",date1);
        model.addAttribute("phone",picture);
        model.addAttribute("brand",carBrands);
        model.addAttribute("style",carStyle);
        model.addAttribute("infor",carDetail1);
        model.addAttribute("seller",seller);

        return "carDesc/infor";
    }

    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页砍价表的插入
    *@Date 22:07 2020/10/8
    *@Param [carCart]
    *@return car.aistar.dto.Result
    **/
    @GetMapping("/kanjia")
    public @ResponseBody
    Result kanjia(CarCart carCart){
       carCartService.insert(carCart);
       if (carCartService.queryAll(carCart)!=null) {
           return new Result("200", "success", carCart);
       }else {
           return new Result("400","no",carCart);
       }

    }
    /**
    *@Author 肖相荣
    *@Description //TODO 车辆详情页预约表的操作
    *@Date 22:07 2020/10/8
    *@Param [id, resTime]
    *@return car.aistar.dto.Result
    **/
    @GetMapping("/yuyue")
    public @ResponseBody
    Result yuyue(String uId, String cId, String selId, String resTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Reserve reserve1 = reserveService.queryByUId(Long.valueOf(uId),Long.valueOf(cId),Long.valueOf(selId));

        try {
            if (new Date().before(sdf.parse(resTime))){
                Reserve reserve = new Reserve();
                reserve.setUId(Long.valueOf(uId));
                reserve.setSId(Long.valueOf(selId));
                reserve.setResCarId(Long.valueOf(cId));
                reserve.setResTime(sdf.parse(resTime));
                reserve.setResStatus(0L);
                if (reserve1!=null){
                     if (sdf.format(reserve1.getResTime()).equals(resTime)){
                        return new Result("300","success",null);
                    }else {
                        reserveService.deleteById(reserve1.getId());
                        reserveService.insert(reserve);
                        return new Result("350","",null);
                    }
                }else {
                    reserveService.insert(reserve);
                    return new Result("200","success",reserve);
                }

            }else {
                return new Result("400","success",null);

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;


    }

/**
*@Author 肖相荣
*@Description //TODO 用户收藏
*@Date 19:49 2020/10/21
*@Param
*@return
**/

    @Autowired
    private UserCollectService userCollectService;
    @GetMapping("/collect")
    public @ResponseBody
    Result collect(String uId, String cId, String addTime){


        CarDetail carDetail = carDetailService.queryById(Long.valueOf(cId));
        UserCollect userCollect1 = userCollectService.queryByUId(Long.valueOf(uId),Long.valueOf(cId));

        if (userCollect1 != null) {
            userCollectService.deleteById(userCollect1.getId());
            return new Result("200", "success", "");
        } else {
            UserCollect userCollect = new UserCollect();
            userCollect.setUId(Long.valueOf(uId));
            userCollect.setCId(Long.valueOf(cId));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                userCollect.setAddTime(sdf.parse(addTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            userCollect.setCPhoto(carDetail.getCPhoto());
            userCollectService.insert(userCollect);
            return new Result("200", "success", "");
        }
    }
    @GetMapping("/lindang")
    public @ResponseBody
    Result collect(String uId, String cId){
        PriceCut priceCut = priceCutService.queryByUId(Long.valueOf(uId),Long.valueOf(cId));
        if (priceCut == null) {
            PriceCut priceCut1 = new PriceCut();
            priceCut1.setCarId(Long.valueOf(cId));
            priceCut1.setUserId(Long.valueOf(uId));
            priceCutService.insert(priceCut1);
            return new Result("200", "success", "");
        }else  if (priceCut != null){
            priceCutService.deleteById(priceCut.getId());
        }
        return new Result("300", "success", "");
    }


}
